# -*- coding: utf-8 -*-
from django import forms
from news.models import News, Category

class CreateForm(forms.ModelForm):
    class Meta:
        model = News

    def __init__(self, *args, **kwargs):
        super(CreateForm, self).__init__(*args, **kwargs)
        user = self.initial['user']
        if not user.is_superuser:
            qs = Category.objects.filter(operator=user)
            self.fields['category'].queryset = qs
            if qs.count() == 1:
                self.fields['category'].initial = qs[0].pk

    def clean_category(self):
        user = self.initial['user']
        data = self.cleaned_data['category']
        if user.is_superuser:
            return data
        try:
            Category.objects.get(pk = data.pk, operator=user)
            return data
        except Category.DoesNotExist:
            raise forms.ValidationError(u'У Вас не хватает прав для добавления в эту категорию! Вы выбрали [%s]' % data)


class CFormCreate(forms.ModelForm):
    class Meta:
        model = Category